package com.invengo.module_check.ui.fragment.search;

import static me.goldze.mvvmhabit.utils.constant.Constants.TOKEN_ADD_BOOKS;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;
import com.invengo.module_check.ui.fragment.search.item.BookAddItemViewModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class BookAddViewModel extends BaseViewModel<BusinessRepsitory> {

    public List<BookBean> selectList = new ArrayList<>();
    public SingleLiveEvent<Integer> selectCount = new SingleLiveEvent<>();

    public ObservableList<BookAddItemViewModel> addBookItemList = new ObservableArrayList<>();
    public ItemBinding<BookAddItemViewModel> addBookitemBinding = ItemBinding.of(BR.viewModel, R.layout.item_search_add);

    public BookAddViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
        selectCount.postValue(0);
        Messenger.getDefault().register(this, TOKEN_ADD_BOOKS, String.class, keywords -> {
            getBooksByExcel(keywords);
        });
    }

    public int getPosition(ItemViewModel item) {
        return addBookItemList.indexOf(item);
    }


    public void getBooksByExcel(String barCode) {
        model.getBookByCode(barCode)
                .compose(RxUtils.applyIOThread(getLifecycleProvider(), this))
                .subscribe(bean -> {
                    if (bean.getData() != null)
                        addItem(bean.getData());
                });
    }

    public void getBooksByKeyword(String keyword) {
        model.getBookByKeyword(keyword, 1, 20)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBeans -> {
                    for (BookBean bookBean : bookBeans) {
                        addItem(bookBean);
                    }
                });
    }
    private List<String> tidList =new ArrayList<>();
    /**
     * 添加项目
     *
     * @param bean 豆
     */

    private void addItem(BookBean bean) {
        if (!tidList.contains(bean.getTid())){
            addBookItemList.add(new BookAddItemViewModel(this,bean));
            tidList.add(bean.getTid());
        }
    }

    public void itemCheckChange(BookBean bean, Boolean ischecked) {
        if (ischecked) {
            selectList.add(bean);
        } else {
            selectList.remove(bean);
        }
        KLog.e("单选按钮 " + selectList.size() + "  " + addBookItemList.size());
        selectCount.postValue(selectList.size());
    }



    public void selectAll() {
        boolean b = selectCount.getValue() == addBookItemList.size();
        selectList.clear();
        for (BookAddItemViewModel bookAddItemViewModel : addBookItemList) {
            bookAddItemViewModel.isSelect.postValue(!b);
            if (!b) {
                selectList.add(bookAddItemViewModel.book.getValue());
                KLog.e("添加元素" + selectList.size());
            }
        }
        KLog.e("单选按钮 " + selectList.size() + "  " + addBookItemList.size());
        selectCount.postValue(selectList.size());
    }

    public BindingCommand<String> searchBookCommand = new BindingCommand<>(s -> {
        getUC().getHideKeyboardEvent().call();
        getBooksByKeyword(s);
    });

    private String keyword = "";
    public BindingCommand<String> textChange = new BindingCommand<>(s -> {
        keyword = s;
    });

    public BindingCommand searchBook = new BindingCommand<>(() -> {
        getUC().getHideKeyboardEvent().call();
        if (TextUtils.isEmpty(keyword)) {
            ToastUtils.showShort("请先输入需要查找的数据");
            return;
        }
        getBooksByKeyword(keyword);
    });

    public BindingCommand selectFinish = new BindingCommand(() -> {
        RxBus.getDefault().post(selectList);
        getUC().getFinishEvent().call();
    });
}
